home *** CD-ROM | disk | FTP | other *** search
/ Linux Programs 1995 Summer / Linux Programs.iso / ghostscr / gs_vgali < prev    next >
Text File  |  1995-04-15  |  11KB  |  409 lines

  1. (provided by permission of the author, Sigfrid Lundberg, 
  2. siglun@euler.teorekol.lu.se. This "patch" is for gs 2.5.2
  3. but I've heard it can be used in 2.6 without much trouble.
  4. I'm only posting it because I had it and people always ask for it.)
  5.  
  6. Andy Tefft
  7. teffta@cs690-3.erie.ge.com
  8.  
  9.  
  10. p.s. Matthias is not the author, just the guy who sent this to me!
  11.  
  12. From:    THOMAS::"A8411GAC@AWIUNI11.EDVZ.UniVie.AC.AT" "Matthias Burian" 16-JUN-1993 03:19:23.06
  13. To:    Andrew Tefft <engr::teffta>
  14. CC:    
  15. Subj:    Re: GhostScript with LINUX device
  16.  
  17.  
  18.  
  19. Hi, Andrew,
  20.  
  21. I've read your message in c.o.l.
  22.  
  23. Some days ago I had the same problem to display some PostScript files
  24. w/o X11, and a kind soul sent me some patches for the ghostscript sources
  25. (ghostscript 2.52).
  26. These are only two files, one called gdevlinx.c, with the vgalib device,
  27. and a small piece to be put in the Makefile.
  28. Maybe you have to twiddle around a little bit, but I didn't try to rebuild
  29. the whole GhostScript.
  30. I'll forward it to you...
  31.                  ...Matthias
  32.                                                   ________
  33.  ________________________________________________|        |___________
  34. |o o o o o o o o o o o o o o o o o o o o o o o o |        |o o o o o o|
  35. |                    /\                          |        |           |
  36. |      /\           /  \/\            /\    /\   |        |           |
  37. |     /  \    /\   /      \      /\  /  \  /  +-----o     |           |
  38. |____/____\__/__\_/________\____/__\/____\/______|        |           |
  39. |          \/               \  /                 |        |           |
  40. |                            \/                  |        |           |
  41. |o o o o o o o o o o o o o o o o o o o o o o o o |        |o o o o o o|
  42. |------------------------------------------------|________|-----------|
  43. |  Matthias Burian                  Institut fuer Analytische Chemie  |
  44. |                                                  Universitaet Wien  |
  45. |  a8411gac@awiuni11.edvz.univie.ac.at        Waehringererstrasse 38  |
  46. |                                               1090 Wien    AUSTRIA  |
  47. |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'
  48.  
  49. *------- cut here ------ gdevlinx.c ------ cut here ------ gdevlinx.c -----*
  50.  
  51.  
  52.  
  53.  
  54.  
  55. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  56.    Distributed by Free Software Foundation, Inc.
  57.  
  58. This file is part of Ghostscript.
  59.  
  60. Ghostscript is distributed in the hope that it will be useful, but
  61. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  62. to anyone for the consequences of using it or for whether it serves any
  63. particular purpose or works at all, unless he says so in writing.  Refer
  64. to the Ghostscript General Public License for full details.
  65.  
  66. Everyone is granted permission to copy, modify and redistribute
  67. Ghostscript, but only under the conditions described in the Ghostscript
  68. General Public License.  A copy of this license is supposed to have been
  69. given to you along with Ghostscript so you can know your rights and
  70. responsibilities.  It should be in a file named COPYING.  Among other
  71. things, the copyright notice and this notice must be preserved on all
  72. copies.  */
  73.  
  74. /*
  75.  * gdevlinux.c
  76.  *
  77.  * This is a driver for 386 PCs using VGALIB for graphics on the console
  78.  * display.
  79.  *
  80.  * Written by Sigfrid Lundberg, siglun@euler.teorekol.lu.se.
  81.  */
  82.  
  83. #include "gx.h"
  84. #include "gxdevice.h"
  85. #include "gserrors.h"
  86.  
  87. #include <errno.h>
  88. #include <vga.h>
  89.  
  90. typedef struct gx_device_linux {
  91.     gx_device_common;
  92.     int fd;                   /* window file descriptor */
  93.     uchar *screen;               /* pointer to screen image */
  94.     ushort line_size;               /* size of single screen line in bytes */
  95.     ulong screen_size;               /* size of screen image in bytes */
  96.     int screen_width, screen_height;   /* size of screen used */
  97.     int page;                   /* page number */
  98. #ifdef LINUX_PERF
  99.     char *no_output, *no_fill, *no_copy;
  100. #endif
  101. } gx_device_linux;
  102.  
  103. #define linuxdev ((gx_device_linux *)dev)
  104.  
  105. #define XDPI   60      /* to get a more-or-less square aspect ratio */
  106. #define YDPI   60
  107.  
  108. #ifndef A4 /*Letter size*/
  109. #define YSIZE (20.0 * YDPI / 2.5)
  110. #define XSIZE (8.5 / 11)*YSIZE /* 8.5 x 11 inch page, by default */
  111. #else                       /* A4 paper */
  112. #define XSIZE 8.27
  113. #define YSIZE 11.69
  114. #endif
  115.  
  116. dev_proc_open_device(linux_open);
  117. dev_proc_close_device(linux_close);
  118. dev_proc_draw_line(linux_draw_line);
  119. dev_proc_fill_rectangle(linux_fill_rectangle);
  120. dev_proc_tile_rectangle(linux_tile_rectangle);
  121. dev_proc_map_color_rgb(linux_map_color_rgb);
  122. dev_proc_map_rgb_color(linux_map_rgb_color);
  123. dev_proc_copy_mono(linux_copy_mono);
  124. dev_proc_copy_color(linux_copy_color);
  125.  
  126.  
  127.  
  128. private gx_device_procs linux_procs = {
  129.     linux_open,
  130.     gx_default_get_initial_matrix,
  131.     gx_default_sync_output,
  132.     gx_default_output_page,
  133.     linux_close,
  134.     linux_map_rgb_color,
  135.     linux_map_color_rgb,
  136.     linux_fill_rectangle,
  137.     linux_tile_rectangle,
  138.     linux_copy_mono,
  139.     linux_copy_color,
  140.     linux_draw_line,
  141.     gx_default_get_bits,
  142.     gx_default_get_props,
  143.     gx_default_put_props
  144. };
  145.  
  146. gx_device_linux gs_linux_device = {
  147.     sizeof(gx_device_linux),
  148.     &linux_procs,
  149.     "linux",
  150.     0,0,
  151.     1,1,
  152.     no_margins,
  153.     dci_black_and_white,
  154.     0
  155. };
  156.  
  157. int linux_open(gx_device *dev)
  158. {
  159.     vga_setmode(G1024x768x256);
  160.     vga_clear();
  161.     if ( dev->width == 0 )
  162.     dev->width = vga_getxdim() + 1;
  163.     if ( dev->height == 0 )
  164.     dev->height = vga_getydim() + 1;
  165.  
  166.   /*vgalib provides no facilities for finding out aspect ratios*/
  167.     if ( dev->y_pixels_per_inch == 1 )
  168.     {
  169.     dev->y_pixels_per_inch = dev->height / 11.0;
  170.     dev->x_pixels_per_inch = dev->y_pixels_per_inch;
  171.     }
  172.  
  173.                     /* Find out if the device supports color */
  174.                   /* (default initialization is monochrome). */
  175.                 /* We only recognize 16-color devices right now. */
  176.     if ( vga_getcolors() > 1 )
  177.     {
  178.  
  179.     int index,one,rgb[3];
  180.  
  181.     static gx_device_color_info linux_16color = dci_color(4, 2, 3);
  182.     dev->color_info = linux_16color;
  183.  
  184.     for(index=0;index<gx_max_color_value;index++)
  185.     {
  186.         one = (index & 8 ? gx_max_color_value : gx_max_color_value / 3);
  187.         rgb[0] = (index & 4 ? one : 0);
  188.         rgb[1] = (index & 2 ? one : 0);
  189.         rgb[2] = (index & 1 ? one : 0);
  190.         vga_setpalette((int)index, rgb[0], rgb[1], rgb[2]);
  191.     }
  192.     }
  193.  
  194.   return 0;
  195. }
  196.  
  197. int linux_close(gx_device *dev)
  198. {
  199.     vga_getch();
  200.     vga_setmode(TEXT);
  201.     return 0;
  202. }
  203.  
  204. gx_color_index linux_map_rgb_color(gx_device *dev, gx_color_value red,
  205.                    gx_color_value green, gx_color_value blue)
  206. {
  207.     int index;
  208.  
  209.     index=((red > gx_max_color_value / 4 ? 4 : 0) +
  210.        (green > gx_max_color_value / 4 ? 2 : 0) +
  211.        (blue > gx_max_color_value / 4 ? 1 : 0) +
  212.        (red > gx_max_color_value / 4 * 3 ||
  213.         green > gx_max_color_value / 4 * 3 ? 8 : 0));
  214.  
  215.     return (gx_color_index)index;
  216. }
  217.  
  218.  
  219. /* I actually don't understand what I'm doing -- the only thing I want to
  220. achieve are palettes that emulates BGI */
  221. int linux_map_color_rgb(gx_device *dev, gx_color_index index,
  222.             unsigned short rgb[3])
  223. {
  224.     int red, green, blue;
  225.     gx_color_value one =
  226.     (index & 8 ? gx_max_color_value : gx_max_color_value / 3);
  227.     rgb[0] = (index & 4 ? one : 0);
  228.     rgb[1] = (index & 2 ? one : 0);
  229.     rgb[2] = (index & 1 ? one : 0);
  230.  
  231.     return 0;
  232. }
  233.  
  234. int linux_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  235.             gx_color_index color)
  236. {
  237.     if(!((x0==x1)&&(y0==y1)))
  238.     {
  239.     vga_setcolor((int)color);
  240.     vga_drawline(x0,y0,x1,y1);
  241.     }
  242.     return 0;
  243. }
  244.  
  245. int linux_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  246.              int x, int y, int w, int h, gx_color_index czero,
  247.              gx_color_index cone, int px, int py)
  248. {
  249.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  250.     {
  251.     linux_fill_rectangle(dev, x, y, w, h, czero);
  252.     czero = gx_no_color_index;
  253.     }
  254.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px,
  255. py);
  256. }
  257.  
  258. int linux_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  259.              gx_color_index color)
  260. {
  261.     int i,j;
  262.  
  263.     fit_fill(dev, x, y, w, h);
  264.     vga_setcolor((int)color);
  265.     for(i=y;i<y+h;i++)
  266.     for(j=x;j<x+w;j++)
  267.         vga_drawpixel(j,i);
  268.     return 0;
  269. }
  270.  
  271. int linux_copy_mono(gx_device *dev, const byte *base, int sourcex,
  272.             int raster, gx_bitmap_id id, int x, int y, int width,
  273.             int height, gx_color_index zero, gx_color_index one)
  274.  
  275. {
  276.     const byte *ptr_line = base + (sourcex >> 3);
  277.     int left_bit = 0x80 >> (sourcex & 7);
  278.     int dest_y = y, end_x = x + width;
  279.     int invert = 0;
  280.     int color;
  281.     int i;
  282.  
  283.     fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  284.  
  285.     if ( zero == gx_no_color_index )
  286.     {
  287.     if ( one == gx_no_color_index )
  288.         return 0;
  289.     color = (int)one;
  290.     }
  291.     else
  292.     {
  293.     if ( one == gx_no_color_index )
  294.     {
  295.         color = (int)zero;
  296.         invert = -1;
  297.     }
  298.     else
  299.     {                   /* Pre-clear the rectangle to zero */
  300.         vga_setcolor(zero);
  301.         linux_fill_rectangle(dev,x,y,width,height,zero);
  302.         color = (int)one;
  303.     }
  304.     }
  305.  
  306.     vga_setcolor(color);
  307.  
  308.     while( height-- )
  309.     {                       /* for each line */
  310.     const byte *ptr_source = ptr_line;
  311.     register int dest_x = x;
  312.     register int bit = left_bit;
  313.  
  314.     while ( dest_x < end_x )
  315.     {                   /* for each bit in the line */
  316.         if ( (*ptr_source ^ invert)  & bit )
  317.         vga_drawpixel(dest_x,dest_y);
  318.         dest_x++;
  319.         if ( (bit >>= 1) == 0 )
  320.         bit = 0x80, ptr_source++;
  321.     }
  322.  
  323.     dest_y++;
  324.     ptr_line += raster;
  325.     }
  326.     return 0;
  327. }
  328.  
  329.  
  330. /* Copy a color pixel map.  This is just like a bitmap, except that */
  331. /* each pixel takes 4 bits instead of 1 when device driver has color. */
  332. int linux_copy_color(gx_device *dev, const byte *base, int sourcex,
  333.              int raster, gx_bitmap_id id, int x, int y,
  334.              int width, int height)
  335. {
  336.  
  337.     fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  338.  
  339.     if ( gx_device_has_color(dev) )
  340.     {                       /* color device, four bits per pixel */
  341.     const byte *line = base + (sourcex >> 1);
  342.     int dest_y = y, end_x = x + width, i;
  343.  
  344.     if ( width <= 0 )
  345.         return 0;
  346.     while ( height-- )
  347.     {                   /* for each line */
  348.         const byte *source = line;
  349.         register int dest_x = x;
  350.  
  351.         if ( sourcex & 1 )
  352.         {                   /* odd nibble first */
  353.         int color =  *source++ & 0xf;
  354.         vga_setcolor(color);
  355.         vga_drawpixel(dest_x,dest_y);
  356.         dest_x++;
  357.         }
  358.                        /* Now do full bytes */
  359.         while ( dest_x < end_x )
  360.         {
  361.         int color = *source >> 4;
  362.         vga_setcolor(color);
  363.         vga_drawpixel(dest_x,dest_y);
  364.         dest_x++;
  365.  
  366.         if ( dest_x < end_x )
  367.         {
  368.             color =  *source++ & 0xf;
  369.             vga_setcolor(color);
  370.             vga_drawpixel(dest_x,dest_y);
  371.             dest_x++;
  372.         }
  373.         }
  374.  
  375.         dest_y++;
  376.         line += raster;
  377.     }
  378.     }
  379.     else
  380.     {                  /* monochrome device: one bit per pixel */
  381.         /* bitmap is the same as bgi_copy_mono: one bit per pixel */
  382.     linux_copy_mono(dev, base, sourcex, raster, id, x, y, width, height,
  383.             (gx_color_index)0, (gx_color_index)7);
  384.     }
  385.  
  386.     return 0;
  387. }
  388.  
  389. *------- cut here ------ part to be inserted in the Makefile --- cut here -*
  390.  
  391.  
  392.  
  393.  
  394. # Choose the device(s) to include.  See devs.mak for details.
  395. #DEVICE_DEVS=x11.dev
  396. DEVICE_DEVS=linux.dev # and whatever drivers you'll need
  397.  
  398. ###### ------------------- Linux PC with vgalib ---------------------- ######
  399. ### User supplied driver                          ###
  400.  
  401. linux_=gdevlinux.$(OBJ)
  402. linux.dev: $(linux_)
  403.    $(SHP)gssetdev linux $(linux_)
  404.    $(SHP)gsaddmod linux -lib vga
  405. gdevlinux.$(OBJ): gdevlinux.c $(GDEV)
  406.  
  407. *------ end of file ------ end of file ------ end of file --------------*
  408.  
  409.